﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.IO;

namespace SmallTalker
{
    /// <summary>
    /// Interaction logic for asker.xaml
    /// </summary>
    public partial class asker : Window
    {
        public asker()
        {
            InitializeComponent();
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct MARGINS
        {
            public int cxLeftWidth;      // width of left border that retains its size
            public int cxRightWidth;     // width of right border that retains its size
            public int cyTopHeight;      // height of top border that retains its size
            public int cyBottomHeight;   // height of bottom border that retains its size
        };

        [DllImport("DwmApi.dll")]
        private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);
                IntPtr myHwnd = windowInteropHelper.Handle;
                HwndSource mainWindowSrc = System.Windows.Interop.HwndSource.FromHwnd(myHwnd);

                mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);

                MARGINS margins = new MARGINS()
                {
                    cxLeftWidth = -1,
                    cxRightWidth = -1,
                    cyBottomHeight = -1,
                    cyTopHeight = -1
                };

                DwmExtendFrameIntoClientArea(myHwnd, ref margins);
                MouseDown += delegate { DragMove(); };
            }
            catch
            {
                MessageBox.Show("OK, we're really srry, but SAL has failed... will u ever forgive us?!? R we going to lose a customer? Oh no, we can't do that! Oh, what are we going to do? Oh my gosh... I just can't bear it ;-( WA WA WA");
                this.Close();
            }
            //real code starts now ;P
            try
            {
                if (Environment.OSVersion.Version.Major < 6) //will handle non aero-compliant Operating systems (think the 50% markt share XP still has...)
                {
                    this.Background = Brushes.Honeydew; //change this if u want
                }
                textBlock1.Text = salnxt.smalltalker.asker(RandomNumber(1, 3));
                textBox1.SelectAll();
                Properties.Settings.Default.timesasked++;
                Properties.Settings.Default.Save();
                if (Properties.Settings.Default.timesasked > 100 & RandomNumber(1, 10) == 5)
                {
                }
                else if (Properties.Settings.Default.timesasked > 500 & RandomNumber(1, 100) == 50)
                {
                }
                else if (Properties.Settings.Default.timesasked < 100)
                {
                }
                else if (RandomNumber(1, 20) == 12)
                {
                    button1.Opacity = 0;
                    button1.IsEnabled = false;
                    button2.Opacity = 0;
                    button2.IsEnabled = false;
                    button3.Opacity = 0;
                    button3.IsEnabled = false;
                    button4.Opacity = 0;
                    button4.IsEnabled = false;
                    button5.IsDefault = true;
                    button5.IsEnabled = true;
                }
                else
                {
                    button1.Opacity = 0;
                    button1.IsEnabled = false;
                    button2.Opacity = 0;
                    button2.IsEnabled = false;
                    button3.Opacity = 0;
                    button3.IsEnabled = false;
                    button4.Opacity = 0;
                    button4.IsEnabled = false;
                    button5.IsDefault = true;
                    button5.IsEnabled = true;
                }
            }
            catch
            {
                MessageBox.Show("error setup");
                this.Close();
            }
        }
        private int RandomNumber(int min, int max)
        {
            try
            {
                Random random = new Random();
                return random.Next(min, max);
            }
            catch
            {
                MessageBox.Show("OK, this is the LAST STRAW! I'm not gonna go around answering you stupid questions without any compensation... Srry, SAL is encountering some, echm, 'technical difficulties,' and the team would LOVE it if u could contact them and tell them 'error ran#' have fun once SAL comes to ;P");
                this.Close();
                return 0;
            }
        }
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("As we learn more about you, you'll see this less and less.");
        }
        public void happyadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("happysent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            int yx = 0;
            foreach (string i in x)
            {
                StreamReader file2 = new StreamReader("happywords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("happywords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    yx++;
                    file.Close();
                }
            }
        }
        public void sadadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("sadsent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            int yx = 0;
            foreach (string i in x)
            {
                StreamReader file2 = new StreamReader("sadwords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("sadwords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    yx++;
                    file.Close();
                }
            }
        }
        public void neutadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("neutsent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            int yx = 0;
            foreach (string i in x)
            {

                StreamReader file2 = new StreamReader("neutralwords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("neutralwords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    yx++;
                    file.Close();
                }
            }
        }
        public int sadchecker(string xy)
        {
            string[] x = xy.Split(' ');
            int yx = 0;
            int howsad = 0;
            StreamReader hapsent = new StreamReader("sadsent.txt");
            if (hapsent.ReadToEnd().ToLower().Contains(xy))
            {
                howsad = 1000000;
            }
            hapsent.Close();
            StreamReader file2 = new StreamReader("sadwords.txt", true);
            foreach (string i in x)
            {
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    howsad++;
                }
            }
            file2.Close();
            return howsad;
        }
        public int happychecker(string xy)
        {
            string[] x = xy.Split(' ');
            int yx = 0;
            int howhappy = 0;
            StreamReader file2 = new StreamReader("happywords.txt");
            StreamReader hapsent = new StreamReader("happysent.txt");
            if (hapsent.ReadToEnd().Contains(xy))
            {
                howhappy = 1000000;
            }
            hapsent.Close();
            foreach (string i in x)
            {
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    howhappy++;
                }
            }
            file2.Close();
            return howhappy;
        }
        public int neutralchecker(string xy)
        {
            string[] x = xy.Split(' ');
            int howneutral = 0;
            StreamReader hapsent = new StreamReader("neutsent.txt");
            if (hapsent.ReadToEnd().Contains(xy))
            {
                howneutral = 1000000;
            }
            hapsent.Close();
            int yx = 0;
            StreamReader file2 = new StreamReader("neutralwords.txt");
            foreach (string i in x)
            {
                if (file2.ReadToEnd().Contains(x[yx]))
                {
                    howneutral++;
                }
            }
            file2.Close();
            return howneutral;
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            happyadder();
            textBox1.Text = "";
            this.Close();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            neutadder();
            textBox1.Text = "";
            this.Close();
        }
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            sadadder();
            textBox1.Text = "";
            this.Close();
        }

        private void analisys(object sender, RoutedEventArgs e)
        {
            int s = sadchecker(textBox1.Text);
            int n = neutralchecker(textBox1.Text);
            int h = happychecker(textBox1.Text);
            if (s > n & s > h)
            {
                if (h / s > .6 || h/n > .6)
                {
                    sadadder();
                }
            }
            else if (n > s & n > h)
            {
                if (h / n > .6 || s / n > .6)
                {
                    neutadder();
                }
            }
            else if (h > s & h > n)
            {
                if (s / h > .6 || n / h > .6)
                {
                    happyadder();
                }
            }
            textBox1.Text = "";
        }
    }
}
